package com.itheima.controller;

import com.itheima.exception.CustomException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@RestControllerAdvice
public class CustomExceptionResolver {
    @ExceptionHandler(CustomException.class)
    public ModelAndView customExceptionHandler(CustomException exception) {
        exception.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errMsg",exception.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandle(Exception exception) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errMsg","系统错误,请稍后重试");
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
